Skip to content

Fix - Avoid SQL warning when no user session is active during plugin init#1179

Merged
Rom1-B merged 5 commits intomainfrom
fix/entity-restrict-criteria-without-session
Apr 16, 2026
Merged

Fix - Avoid SQL warning when no user session is active during plugin init#1179
Rom1-B merged 5 commits intomainfrom
fix/entity-restrict-criteria-without-session

Conversation

@RomainLvr
Copy link
Copy Markdown
Contributor

Checklist before requesting a review

Please delete options that are not relevant.

  • I have performed a self-review of my code.
  • I have added tests (when available) that prove my fix is effective or that my feature works.
  • I have updated the CHANGELOG with a short functional description of the fix or new feature.
  • This change requires a documentation update.

Description

  • It fixes !43370
  • Here is a brief description of what this PR does

When initializing the plugin during boot (before any user session), getEntitiesRestrictCriteria() was called without an active session, producing entities_id = '' in the SQL query and triggering a mass MySQL warning (1292: Truncated incorrect DECIMAL value).

Fix by skipping entity restriction when no user is logged in (!Session::getLoginUserID()), consistent with the existing check already in place in setup.php.

Error :

SQL: SELECT * FROM `glpi_plugin_fields_containers` WHERE `is_active` = '1' AND `type` = 'dom' AND ((`itemtypes` LIKE '%\\\"Ticket\\\"%') OR (`itemtypes` LIKE '%\\\"Change\\\"%') OR (`itemtypes` LIKE '%\\\"Problem\\\"%')) AND (`glpi_plugin_fields_containers`.`entities_id` = '') ORDER BY `name`
  Warnings: 
1292: Truncated incorrect DECIMAL value: '' at DBmysql.php line 444
  Backtrace :
  ./src/DBmysql.php:444                              
  ./src/DBmysqlIterator.php:129                      DBmysql->doQuery()
  ./src/DBmysql.php:1088                             DBmysqlIterator->execute()
  ./src/CommonDBTM.php:632                           DBmysql->request()
  ./plugins/fields/inc/questiontype.class.php:384    CommonDBTM->find()
  ./plugins/fields/inc/questiontype.class.php:426    PluginFieldsQuestionType->getAvailableBlocks()
  ./plugins/fields/setup.php:428                     PluginFieldsQuestionType::hasAvailableFields()
  ./plugins/fields/setup.php:136                     plugin_fields_register_plugin_types()
  ./src/Plugin.php:475                               plugin_init_fields()
  ./src/Plugin.php:428                               Plugin::load()
  ...tener/PostBootListener/InitializePlugins.php:75 Plugin->init()
  .../event-dispatcher/Debug/WrappedListener.php:116 Glpi\Kernel\Listener\PostBootListener\InitializePlugins->onPostBoot()
  ...ymfony/event-dispatcher/EventDispatcher.php:220 Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke()
  ...symfony/event-dispatcher/EventDispatcher.php:56 Symfony\Component\EventDispatcher\EventDispatcher->callListeners()
  ...spatcher/Debug/TraceableEventDispatcher.php:142 Symfony\Component\EventDispatcher\EventDispatcher->dispatch()
  ./src/Glpi/Kernel/Kernel.php:149                   Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch()
  ./vendor/symfony/http-kernel/Kernel.php:201        Glpi\Kernel\Kernel->boot()
  ./public/index.php:71                              Symfony\Component\HttpKernel\Kernel->handle()

Close #1172

Comment thread inc/questiontype.class.php Outdated
@RomainLvr RomainLvr requested a review from Rom1-B April 15, 2026 09:45
Comment thread inc/questiontype.class.php Outdated
Comment on lines +383 to +393
$entity_restrict = isCommandLine() ? [] : getEntitiesRestrictCriteria(PluginFieldsContainer::getTable(), '', '', true);

// If entity restriction contains an empty string value, it means no valid session
// is active. Running the query would produce a MySQL
// warning (1292: Truncated incorrect DECIMAL value)
foreach ($entity_restrict as $criterion) {
if (is_array($criterion) && in_array('', $criterion, true)) {
return $available_blocks;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like this:

Suggested change
$entity_restrict = isCommandLine() ? [] : getEntitiesRestrictCriteria(PluginFieldsContainer::getTable(), '', '', true);
// If entity restriction contains an empty string value, it means no valid session
// is active. Running the query would produce a MySQL
// warning (1292: Truncated incorrect DECIMAL value)
foreach ($entity_restrict as $criterion) {
if (is_array($criterion) && in_array('', $criterion, true)) {
return $available_blocks;
}
}
$activeentities = getEntitiesRestrictCriteria(PluginFieldsContainer::getTable(), '', '', true);
if ($activeentities === []) {
return [];
}
$entity_restrict = isCommandLine() ? [] : $activeentities;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getEntitiesRestrictCriteria returns an array of criteria. The problem here isn't that getEntitiesRestrictCriteria returns an empty array; rather, it returns the criterion glpi_plugin_fields_containers.entities_id = "", so the suggestion won't fix the problem.

Copy link
Copy Markdown
Contributor

@Rom1-B Rom1-B Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true, but ultimately I think the problem lies in the getEntitiesRestrictCriteria() function, which returns an invalid value. Instead of returning ...entities_id = '' (which can never work), it should return [new QueryExpression(‘false’)];
For your information, it also checks isCommandLine(), so the one here will likely be redundant.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, the root cause is actually in the GLPI core — DbUtils::getEntitiesRestrictCriteria() in DbUtils.php.

When called with no active session, no CLI context, and no cron context, the function falls through all conditions without an else branch, leaving $value as an empty string ''. This produces an invalid SQL criterion entities_id = '' on an integer column, which triggers MySQL warning.

I've updated this with a cleaner guard:

if (!Session::getLoginUserID() && !isCommandLine()) {
    return $available_blocks;
}

This is semantically explicit (no session = no available blocks), consistent with the existing guard already in place in setup.php l.138 — it doesn't rely on inspecting the internal structure of the criteria returned by core.

The real fix should live in the core: adding an else { return [new QueryExpression('false')]; } branch to getEntitiesRestrictCriteria() so all callers are protected. I'll open a separate PR on the GLPI core for that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the fix for the core : glpi-project/glpi#23934

@RomainLvr RomainLvr requested a review from Rom1-B April 15, 2026 15:27
@Rom1-B Rom1-B merged commit 74f09a8 into main Apr 16, 2026
3 checks passed
@Rom1-B Rom1-B deleted the fix/entity-restrict-criteria-without-session branch April 16, 2026 08:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MySQL warning 1292 spam on boot — getAvailableBlocks() calls getEntitiesRestrictCriteria() before session is initialized

2 participants